package com.ywk.base.concurrency.threadPool;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author zbh
 * @Date 2023/2/19
 */
public class CountDownLatchTest {
    public static void main(String[] args) {
        //创建含有10个线程的线程池
        ExecutorService threadPool= Executors.newFixedThreadPool(10);
        //页数
        Integer totalPage = 10;

        CountDownLatch latch = new CountDownLatch(totalPage);

        //每一页分配一个线程进行处理
        for (int i = 1; i <= totalPage; i++) {
            final Integer page = i;
            threadPool.execute(() -> {
                try {
                    //业务处理
                    System.out.println("开始线程"+Thread.currentThread().getName());
                } finally {
                    latch.countDown();
                }
            });
        }
        System.out.println("等待子线程");
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {

        }
        System.out.println("子线程执行完成");
    }


}
